package de.koller.worlders.engine.entity.enc.gu;

import de.koller.oceanFramework.tree.Mother;
import de.koller.worlders.WSea;
import de.koller.worlders.engine.entity.enc.pathfinding.WayEntity;
import de.koller.worlders.render.IGLRender;
import de.koller.worlders.render.VisualMap;
import java.util.LinkedList;
import java.util.List;
import org.lwjgl.opengl.GL14;

public class GUMovement extends WSea implements IGLRender {
	
	@Mother private GUMob mob;
	private final LinkedList<WayEntity> moveWay = new LinkedList<>();
	private WayEntity destination;

	@Override
	protected void init() {
		super.init();
		
		wroot.rendering.addTransparentWorldElement( this );
	}

	@Override
	protected void release() {
		super.release();
		
		wroot.rendering.removeTransparentWorldlement( this );
	}

	@Override
	protected void update( int elapsedTime ) {
		super.update( elapsedTime );
		
		if( destination == null )
			return;
		
		if( destination.tx == mob.getCentre().getXi() && destination.ty == mob.getCentre().getYi() ) {
			destination = null;
			return;
		}
	}

	public void moveTo( int tx, int ty ) {
		destination = new WayEntity( tx, ty );
		moveWay.clear();
		
		List<WayEntity> w = mob.eroot.pathfinder.way( 
				new WayEntity( mob.getCentre().getXi(), mob.getCentre().getYi() ),
				new WayEntity( tx, ty ),
				mob );
		
		if( w != null )
			moveWay.addAll( w );
		
		if( moveWay.size() >= 1 ) moveWay.removeFirst();
	}
	
	@Override
	public void renderGL( int elapsedTime ) {	
		if( ( mob.eroot.isTargetedMob( mob ) || mob.isPlayerOver() ) == false )
			return;
		
		if( mob.eroot.isTargetedMob( mob ) )
			GL14.glBlendColor( 1, 1, 1, 0.4f );
		else
			GL14.glBlendColor( 1, 1, 1, 0.2f );
			
		renderWay();
	}
	public void renderWay() {
		if( destination == null )
			return;
		
		WayEntity last = null;
		if( !moveWay.isEmpty() ) 
			last = moveWay.getLast();
		
		for( WayEntity we : moveWay )
			if( !we.equals( last ) )
				VisualMap.guWayMove.renderObject( we.tx, we.ty, 1, 1, 10, false );
			else
				VisualMap.guWayDest.renderObject( destination.tx, destination.ty, 1, 1, 10, false );
		
		if( moveWay.isEmpty() )
			VisualMap.guWayErro.renderObject( destination.tx, destination.ty, 1, 1, 10, false );
	}
	
}
